package com.changpay.zywy.ui.activity.machinecontrol
import android.content.Intent
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.mine.PayPasswordActivity
import kotlinx.android.synthetic.main.activity_allocation_detail.*
import kotlinx.android.synthetic.main.title_layout.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import java.util.HashMap


class AllocationDetailActivity : BaseActivity(), IView {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_allocation_detail
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }




    override fun initData() {
        tv_title.text = "调拨详情"
        iv_back.setOnClickListener { finish() }
        val params = HashMap<String, String>()
        params["id"] = intent.getStringExtra("id")
        params["key"] = token
        RetrofitHelper.service.allocationLogInfo(params).sss(this)
        {
            tv_my_name.text = it.result.member_Initiator
            tv_your_name.text = it.result.recipient_name
            tv_phone.text = it.result.recipient_mobile
            tv_sn_number.text = it.result.sn_code
            tv_jiju_name.text = it.result.equipment
            tv_number.text = it.result.equipment_num.toString()
            tv_time.text = it.result.equipment_time
            if (it.result.is_type == "0") {
                tv_take_money.text = "确认收款"
                tv_take_money.visibility = View.GONE
            } else {
                tv_take_money.text = "已收款"
                tv_take_money.isEnabled = false
                tv_take_money.visibility = View.GONE
            }
        }
    }

    override fun initView() {
        iv_back.onClick { finish() }
        tv_take_money.setOnClickListener { ConfirmMoneyOne("确认收款") }
    }

    override fun start() {

    }

    private fun ConfirmMoneyOne( message: String) {
        Intent(this, PayPasswordActivity::class.java).run {
            putExtra("activity_title", "验证支付密码")
            putExtra("title", message)
            putExtra("check_message", " ")
            putExtra("imp_type", 2) // 验证支付密码
            startActivityForResult(this, 100)
        }
    }

    private fun ConfirmMoneyTwo() {
        val params = HashMap<String, String>()
        params["id"] = intent.getStringExtra("id")
        params["key"] = token
        RetrofitHelper.service.allocationConfirm(params).sss(this) {
            showToast(it.result.toString())
            tv_take_money.text = "已收款"
            tv_take_money.isEnabled = false
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            ConfirmMoneyTwo()
        }
    }
}
